package com.lancoo.answer.widget.combinationView;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Looper;
import android.text.Html;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lancoo.answer.R;
import com.lancoo.answer.manager.MediaManager;
import com.lancoo.answer.model.ConstantBean;
import com.lancoo.answer.model.TaskState;
import com.lancoo.answer.model.bean.Child;
import com.lancoo.answer.model.bean.Item;
import com.lancoo.answer.model.entity.TaskControlBean;
import com.lancoo.answer.util.DpUtils;
import com.lancoo.answer.util.ExamFontSizeAdjustUtils;
import com.lancoo.answer.util.paperUtils.ChildRightOrWrongJudgeUtils;
import com.lancoo.answer.widget.span.CenteredImageSpan;
import com.lancoo.answer.widget.span.MatchAnwerFillCharSpan;
import com.lancoo.answer.widget.span.MatchAnwerSpan;

import java.util.List;


/**
 * Created by pengmi on 2021/12/7 11:45
 * Description:阅读匹配小题题干样式
 */
public class ReadingMatchQueseBodyView extends RelativeLayout implements View.OnClickListener{

    private static final String TAG = "MatchQueseBodyView";
    private Child child;
    private TextView tv_queseBody;
    private long subTime;
    private MediaManager mediaManager;
    private final int showIndex=0;
    private int trainType=2;//0--作答样式，1--已提交，2--查看评阅


    public ReadingMatchQueseBodyView(@NonNull Context context) {
        super(context);
        init(context);
    }

    public ReadingMatchQueseBodyView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ReadingMatchQueseBodyView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context){
        LayoutInflater.from(context).inflate(R.layout.ev_layout_quese_body_match_view,this);
        tv_queseBody = findViewById(R.id.tv_quese_body);
        int dp12= DpUtils.dip2px(context,6);
        tv_queseBody.setLineSpacing(dp12,1);
        findViewById(R.id.rl_horn).setOnClickListener(this);
        tv_queseBody.setBackgroundResource(R.drawable.ev_shape_5_corners_f7_bg);
        tv_queseBody.setPadding(dp12,dp12,dp12,dp12);
        adjustFontSize();
    }

    public void initData(Child child){
        if(child==null){
            Log.e(TAG,"child is null");
            return;
        }
        this.child=child;
        findViewById(R.id.child_audio_view).setVisibility(GONE);
        if(trainType==2){
            loadLookingStyle();
        }else if(trainType==3){
            loadOriginalStyle();
        }else{
            loadTrainStyle();
        }
    }

    private void loadLookingStyle(){
        TaskControlBean taskControlBean = ConstantBean.Companion.getTaskControlBean();

        String childAsk = child.getChildAsk();
        List<Item> itemList = child.getItemList();
        if(itemList==null){
            return;
        }
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
        spannableStringBuilder.append(itemList.get(0).getSortIndex()).append(".");
        spannableStringBuilder.append(" ");

        int index=0;
        String itemAnswer=child.getChildAnswer();
        int type;//0-未填写，1-做对，2-做错
        Item item = child.getItemList().get(0);
        String stuAnswer = item.getStuAnswer();
        if(TextUtils.isEmpty(stuAnswer)){
            type=0;
        }else{
            int state = ChildRightOrWrongJudgeUtils.judateItemAnwerState(item);
            if(state==1){
                type=1;
            }else{
                type=2;
            }
            if(taskControlBean!=null&&taskControlBean.getExpired()){
                type=2;
            }
        }
        switch (type){
            case 0:
                SpannableString ss1=new SpannableString("未");
                MatchAnwerSpan mas1=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                if(taskControlBean!=null&&taskControlBean.getExpired()){
                    mas1.setDrawableLineType(4);
                }else{
                    mas1.setDrawableLineType(1);
                }
                mas1.setmText("未");
                mas1.setEndFill(false);
                mas1.setStartFill(true);
                mas1.setAnswerPointIndex(index);
                ss1.setSpan(mas1,0,ss1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss1);

                SpannableString ss2=new SpannableString("填");
                MatchAnwerSpan mas2=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                if(taskControlBean!=null&&taskControlBean.getExpired()){
                    mas2.setDrawableLineType(4);
                }else{
                    mas2.setDrawableLineType(1);
                }
                mas2.setmText("填");
                mas2.setEndFill(false);
                mas2.setStartFill(false);
                mas2.setAnswerPointIndex(index);
                ss2.setSpan(mas2,0,ss2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss2);

                SpannableString ss3=new SpannableString("写");
                MatchAnwerSpan mas3=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                if(taskControlBean!=null&&taskControlBean.getExpired()){
                    mas3.setDrawableLineType(4);
                }else{
                    mas3.setDrawableLineType(1);
                }
                mas3.setmText("写");
                mas3.setEndFill(false);
                mas3.setStartFill(false);
                mas3.setAnswerPointIndex(index);
                ss3.setSpan(mas3,0,ss3.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss3);

                SpannableString ss4=new SpannableString("(");
                MatchAnwerSpan mas4=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas4.setDrawableLineType(2);
                mas4.setmText("(");
                mas4.setEndFill(false);
                mas4.setStartFill(false);
                mas4.setAnswerPointIndex(index);
                ss4.setSpan(mas4,0,ss4.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss4);

                SpannableString ss5=new SpannableString(""+itemAnswer);
                MatchAnwerSpan mas5=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas5.setDrawableLineType(2);
                mas5.setmText(itemAnswer);
                mas5.setEndFill(false);
                mas5.setStartFill(false);
                mas5.setAnswerPointIndex(index);
                ss5.setSpan(mas5,0,ss5.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss5);

                SpannableString ss6=new SpannableString(")");
                MatchAnwerSpan mas6=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas6.setDrawableLineType(2);
                mas6.setmText(")");
                mas6.setEndFill(true);
                mas6.setStartFill(false);
                mas6.setAnswerPointIndex(index);
                ss6.setSpan(mas6,0,ss6.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss6);

                break;
            case 1:
                SpannableString ss11=new SpannableString("1");
                MatchAnwerSpan mas11=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas11.setDrawableLineType(2);
                mas11.setmText("\t");
                mas11.setEndFill(false);
                mas11.setStartFill(true);
                mas11.setAnswerPointIndex(index);
                ss11.setSpan(mas11,0,ss11.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss11);

                SpannableString ss12=new SpannableString("1");
                MatchAnwerSpan mas12=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas12.setDrawableLineType(2);
                mas12.setmText("\t");
                mas12.setEndFill(false);
                mas12.setStartFill(false);
                mas12.setAnswerPointIndex(index);
                ss12.setSpan(mas12,0,ss12.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss12);

                SpannableString ss13=new SpannableString(""+itemAnswer);
                MatchAnwerSpan mas13=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas13.setDrawableLineType(2);
                mas13.setmText(itemAnswer);
                mas13.setEndFill(false);
                mas13.setStartFill(false);
                mas13.setAnswerPointIndex(index);
                ss13.setSpan(mas13,0,ss13.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss13);

                SpannableString ss14=new SpannableString("1");
                MatchAnwerSpan mas14=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas14.setDrawableLineType(2);
                mas14.setmText("\t");
                mas14.setEndFill(false);
                mas14.setStartFill(false);
                mas14.setAnswerPointIndex(index);
                ss14.setSpan(mas14,0,ss14.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss14);
                SpannableString ss15=new SpannableString("1");
                MatchAnwerSpan mas15=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas15.setDrawableLineType(2);
                mas15.setmText("\t");
                mas15.setEndFill(true);
                mas15.setStartFill(false);
                mas15.setAnswerPointIndex(index);
                ss15.setSpan(mas15,0,ss15.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss15);
                break;
            case 2:
                SpannableString ss21=new SpannableString("1");
                MatchAnwerSpan mas21=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas21.setDrawableLineType(2);
                mas21.setmText("\t");
                mas21.setEndFill(false);
                mas21.setStartFill(true);
                mas21.setAnswerPointIndex(index);
                ss21.setSpan(mas21,0,ss21.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss21);

                SpannableString ss22=new SpannableString("A");
                MatchAnwerSpan mas22=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                if(taskControlBean!=null&&taskControlBean.getExpired()){
                    mas22.setDrawableLineType(4);
                }else{
                    mas22.setDrawableLineType(1);
                }
                mas22.setmText(item.getStuAnswer());
                mas22.setEndFill(false);
                mas22.setStartFill(false);
                mas22.setAnswerPointIndex(index);
                ss22.setSpan(mas22,0,ss22.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss22);

                SpannableString ss231=new SpannableString("(");
                MatchAnwerSpan mas231=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas231.setDrawableLineType(2);
                mas231.setmText("(");
                mas231.setEndFill(false);
                mas231.setStartFill(false);
                mas231.setAnswerPointIndex(index);
                ss231.setSpan(mas231,0,ss231.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss231);

                SpannableString ss23=new SpannableString(""+itemAnswer);
                MatchAnwerSpan mas23=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas23.setDrawableLineType(2);
                mas23.setmText(itemAnswer);
                mas23.setEndFill(false);
                mas23.setStartFill(false);
                mas23.setAnswerPointIndex(index);
                ss23.setSpan(mas23,0,ss23.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss23);

                SpannableString ss232=new SpannableString(")");
                MatchAnwerSpan mas232=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas232.setDrawableLineType(2);
                mas232.setmText(")");
                mas232.setEndFill(false);
                mas232.setStartFill(false);
                mas232.setAnswerPointIndex(index);
                ss232.setSpan(mas232,0,ss232.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss232);

                SpannableString ss24=new SpannableString("1");
                MatchAnwerSpan mas24=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
                mas24.setDrawableLineType(2);
                mas24.setmText("\t");
                mas24.setEndFill(true);
                mas24.setStartFill(false);
                mas24.setAnswerPointIndex(index);
                ss24.setSpan(mas24,0,ss24.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableStringBuilder.append(ss24);

        }
        spannableStringBuilder.append(" ");
        spannableStringBuilder.append(Html.fromHtml(childAsk.replace("____","")));
        tv_queseBody.setText(spannableStringBuilder);
        MatchAnwerSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MatchAnwerSpan.class);
        for (MatchAnwerSpan span : spans) {
            span.setUnderLine(span.getAnswerPointIndex() == showIndex);
        }
        tv_queseBody.setMovementMethod(new LinkMovementMethod());
        tv_queseBody.requestLayout();
    }

    private void loadTrainStyle(){
        String childAsk = child.getChildAsk();
        List<Item> itemList = child.getItemList();
        if(itemList==null){
            return;
        }
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
        spannableStringBuilder.append(addMarkTagImg(getContext(),child));
        Item item = itemList.get(0);
        spannableStringBuilder.append(item.getSortIndex()).append(".");
        spannableStringBuilder.append(" ");
        int index=0;
        String itemAnswer=item.getStuAnswer();
        if(TextUtils.isEmpty(itemAnswer)){
            itemAnswer=" ";
        }

        SpannableString ss11=new SpannableString("1");
        MatchAnwerSpan mas11=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas11.setDrawableLineType(4);
        mas11.setmText("\t");
        mas11.setEndFill(false);
        mas11.setStartFill(true);
        mas11.setAnswerPointIndex(index);
        ss11.setSpan(mas11,0,ss11.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss11);

        SpannableString ss12=new SpannableString("1");
        MatchAnwerSpan mas12=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas12.setDrawableLineType(4);
        mas12.setmText("\t");
        mas12.setEndFill(false);
        mas12.setStartFill(false);
        mas12.setAnswerPointIndex(index);
        ss12.setSpan(mas12,0,ss12.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss12);

        SpannableString ss13=new SpannableString(""+itemAnswer);
        MatchAnwerFillCharSpan mas13=new MatchAnwerFillCharSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas13.setDrawableLineType(4);
        mas13.setmText(itemAnswer);
        mas13.setEndFill(false);
        mas13.setStartFill(false);
        mas13.setAnswerPointIndex(index);
        ss13.setSpan(mas13,0,ss13.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss13);

        SpannableString ss14=new SpannableString("1");
        MatchAnwerSpan mas14=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas14.setDrawableLineType(4);
        mas14.setmText("\t");
        mas14.setEndFill(false);
        mas14.setStartFill(false);
        mas14.setAnswerPointIndex(index);
        ss14.setSpan(mas14,0,ss14.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss14);
        SpannableString ss15=new SpannableString("1");
        MatchAnwerSpan mas15=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas15.setDrawableLineType(4);
        mas15.setmText("\t");
        mas15.setEndFill(true);
        mas15.setStartFill(false);
        mas15.setAnswerPointIndex(index);
        ss15.setSpan(mas15,0,ss15.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss15);
        spannableStringBuilder.append(" ");
        spannableStringBuilder.append(Html.fromHtml(childAsk));
        tv_queseBody.setText(spannableStringBuilder);
        MatchAnwerSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MatchAnwerSpan.class);
        for (MatchAnwerSpan span : spans) {
            span.setUnderLine(span.getAnswerPointIndex() == showIndex);
        }
        tv_queseBody.setMovementMethod(new LinkMovementMethod());
        tv_queseBody.requestLayout();
    }



    public void pause(){
        Log.e(TAG,"pause");
        if(mediaManager!=null){
            mediaManager.pause();
        }
        showPlayState(false);
    }

    public void release(){
        if(mediaManager!=null){
            mediaManager.release();
        }
    }


    @Override
    public void onClick(View v) {
        if(System.currentTimeMillis()-subTime<500){
            return;
        }
        subTime= System.currentTimeMillis();

        if(mediaManager==null||!mediaManager.isPlay()){
            startPlayRecordSound();
            showPlayState(true);
        }else{
            showPlayState(false);
            mediaManager.pause();
        }
    }


    private void showPlayState(boolean isPlaying){
        if( Looper.myLooper() != Looper.getMainLooper()){
            return;
        }
    }


    private void startPlayRecordSound(){
        if(mediaManager==null){
            mediaManager=new MediaManager(getContext());
        }
        String url="";
        mediaManager.setUpMedia(url, new MediaManager.OnPlayListener() {
            @Override
            public void onComplete() {
                showPlayState(false);
            }

            @Override
            public void onStartError() {
                showPlayState(false);
            }

            @Override
            public void onPausePlay() {

            }

            @Override
            public void onSourceError() {
                showPlayState(false);
            }

            @Override
            public void onStopPlay() {

            }

            @Override
            public void onPrepare() {
                if(mediaManager!=null){
                    mediaManager.start();
                }
            }

            @Override
            public void onBufferingUpdate(MediaPlayer mp, int percent) {
                Log.e(TAG,"onBufferingUpdate:"+percent);
            }
        });
    }

    public void setTrainType(int trainType){
        this.trainType=trainType;
    }



    private void loadOriginalStyle(){
        String childAsk = child.getChildAsk();
        List<Item> itemList = child.getItemList();
        if(itemList==null){
            return;
        }
        SpannableStringBuilder spannableStringBuilder=new SpannableStringBuilder();
        spannableStringBuilder.append(itemList.get(0).getSortIndex()).append(".");
        spannableStringBuilder.append(" ");
        int index=0;
        String itemAnswer=child.getChildAnswer();
        SpannableString ss11=new SpannableString("1");
        MatchAnwerSpan mas11=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas11.setDrawableLineType(2);
        mas11.setmText("\t");
        mas11.setEndFill(false);
        mas11.setStartFill(true);
        mas11.setAnswerPointIndex(index);
        ss11.setSpan(mas11,0,ss11.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss11);

        SpannableString ss12=new SpannableString("1");
        MatchAnwerSpan mas12=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas12.setDrawableLineType(2);
        mas12.setmText("\t");
        mas12.setEndFill(false);
        mas12.setStartFill(false);
        mas12.setAnswerPointIndex(index);
        ss12.setSpan(mas12,0,ss12.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss12);

        SpannableString ss13=new SpannableString(""+itemAnswer);
        MatchAnwerSpan mas13=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas13.setDrawableLineType(2);
        mas13.setmText(itemAnswer);
        mas13.setEndFill(false);
        mas13.setStartFill(false);
        mas13.setAnswerPointIndex(index);
        ss13.setSpan(mas13,0,ss13.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss13);

        SpannableString ss14=new SpannableString("1");
        MatchAnwerSpan mas14=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas14.setDrawableLineType(2);
        mas14.setmText("\t");
        mas14.setEndFill(false);
        mas14.setStartFill(false);
        mas14.setAnswerPointIndex(index);
        ss14.setSpan(mas14,0,ss14.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss14);
        SpannableString ss15=new SpannableString("1");
        MatchAnwerSpan mas15=new MatchAnwerSpan(tv_queseBody.getContext(),tv_queseBody.getPaint());
        mas15.setDrawableLineType(2);
        mas15.setmText("\t");
        mas15.setEndFill(true);
        mas15.setStartFill(false);
        mas15.setAnswerPointIndex(index);
        ss15.setSpan(mas15,0,ss15.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        spannableStringBuilder.append(ss15);
        spannableStringBuilder.append(" ");
        spannableStringBuilder.append(Html.fromHtml(childAsk));
        tv_queseBody.setText(spannableStringBuilder);
        MatchAnwerSpan[] spans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), MatchAnwerSpan.class);
        for (MatchAnwerSpan span : spans) {
            span.setUnderLine(span.getAnswerPointIndex() == showIndex);
        }
        tv_queseBody.setMovementMethod(new LinkMovementMethod());
        tv_queseBody.requestLayout();
    }

    public void adjustFontSize(){
        TextView tv_queseBody=findViewById(R.id.tv_quese_body);
        ExamFontSizeAdjustUtils.adjustTextViewFontSize(tv_queseBody,15);
    }

    private static SpannableString addMarkTagImg(Context mContext,Child child){
        if(!child.getItemList().get(0).getItemMark()||ConstantBean.Companion.getTaskState()!= TaskState.TRAINING){
            return new SpannableString("");
        }
        SpannableString spannableString=new SpannableString("123 ");
        spannableString.setSpan(new CenteredImageSpan(mContext, R.drawable.ev_vector_question_yellow),0,3,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public void markQues(){
        if(child==null){
            return;
        }
        List<Item> itemList = child.getItemList();
        Item item = itemList.get(0);
        item.setItemMark(!item.getIsRemark());
        loadTrainStyle();
    }

}
